package com.cet.qz.unit01.chapter1_1;

import edu.princeton.cs.algs4.StdIn;

import java.util.ArrayList;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2020-12-10 21:44
 **/
public class Qz1_1_23 {

    public static int rank(int key,int[] a){
        return rank(key,a,0,a.length-1);
    }

    public static int rank(int key,int[] a,int lo,int hi){
        if(lo > hi)
            return -1;

        int mid = lo + (hi -lo)/2;
        if(key > a[mid])
            return rank(key,a,mid + 1,hi);
        else if(key < a[mid])
            return rank(key,a,lo,mid -1);
        else
            return mid;
    }

    public static void main(String[] args) {
        System.out.println("+ :代表筛选出输入不在白名单上的值");
        System.out.println("- :代表筛选出输入在白名单上的值");
        System.out.println("请选择筛选模式：");
        char model = StdIn.readChar();
        if ('+' == model) {
            //初始化白名单
            int[] init = new int[100];
            for (int i = 0; i < 100; i++) {
                init[i] = i+1;
            }
            //System.out.println(rank(10,init));
            System.out.println("选择完毕，你选择了筛选出输入不在白名单上的值");
            System.out.println("++++++++++++++++++++++++++++++++++++++++");
            System.out.println("请输入待筛选的整数：");
            ArrayList<Integer> inputList = new ArrayList<>();
            while (!StdIn.isEmpty()){
                int input = StdIn.readInt();
                inputList.add(input);
            }
            for (Integer i : inputList) {
                if(rank(i,init) == -1){
                    System.out.println(i);
                }
            }
        } else if ('-' == model) {
            //初始化白名单
            int[] init = new int[100];
            for (int i = 0; i < 100; i++) {
                init[i] = i+1;
            }
            System.out.println("选择完毕，你选择了筛选出输入在白名单上的值");
            System.out.println("--------------------------------------");
            System.out.println("请输入待筛选的整数：");
            ArrayList<Integer> inputList = new ArrayList<>();
            while (!StdIn.isEmpty()){
                int input = StdIn.readInt();
                inputList.add(input);
            }
            for (Integer i : inputList) {
                if(rank(i,init) != -1){
                    System.out.println(i);
                }
            }
        } else {
            System.out.println("输入有误，请重新启动！");
        }



    }
}
